#include "ObjectManager.h"
#include "Tower.h"
#include "Monster.h"
#include "Bullet.h"
#include "Object.h"


void ObjectManager::AddTower(Tower *tower)
{
	m_vTowers.push_back(tower);
}
void ObjectManager::AddMonster(Monster *monster)
{
	m_vMonsters.push_back(monster);
}
void ObjectManager::AddBullet(Bullet *bullet)
{
	m_vBullets.push_back(bullet);
}

Tower* ObjectManager::GetTower(int index)
{
	return m_vTowers[index];
}
Monster* ObjectManager::GetMonster(int index)
{
	return m_vMonsters[index];
}
Bullet* ObjectManager::GetBullet(int index)
{
	return m_vBullets[index];
}


int ObjectManager::GetTowerIndex(Tower* tower)
{
	for(unsigned int i = 0; i < m_vTowers.size(); i++)
	{
		if(m_vTowers[i] == tower)
		{
			return i;
		}
	}

	return -1;
}

int ObjectManager::GetMonsterIndex(Monster* monster)
{
	for(unsigned int i = 0; i < m_vMonsters.size(); i++)
	{
		if(m_vMonsters[i] == monster)
		{
			return i;
		}
	}

	return -1;
}

int ObjectManager::GetBulletIndex(Bullet* bullet)
{
	for(unsigned int i = 0; i < m_vBullets.size(); i++)
	{
		if(m_vBullets[i] == bullet)
		{
			return i;
		}
	}

	return -1;
}


void ObjectManager::DeleteTower(Tower *tower)
{
	int i = GetTowerIndex(tower);
	if(i >= 0)
	{
		m_vTowers[i]->Release();
		delete m_vTowers[i];
		m_vTowers.erase(m_vTowers.begin() + i);
	}
}

void ObjectManager::DeleteMonster(Monster *monster)
{
	int i = GetMonsterIndex(monster);
	if(i >= 0)
	{
		m_vMonsters[i]->Release();
		delete m_vMonsters[i];
		m_vMonsters.erase(m_vMonsters.begin() + i);
	}
}

void ObjectManager::DeleteBullet(Bullet *bullet)
{
	int i = GetBulletIndex(bullet);
	if(i >= 0)
	{
		m_vBullets[i]->Release();
		delete m_vBullets[i];
		m_vBullets.erase(m_vBullets.begin() + i);
	}
}

bool ObjectManager::Release()
{
	for(int i = 0; i < (int)m_vTowers.size(); i++)
	{
		m_vTowers[i]->Release();
		delete m_vTowers[i];
	}

	for(int i = 0; i < (int)m_vMonsters.size(); i++)
	{
		m_vMonsters[i]->Release();
		delete m_vMonsters[i];
	}

	for(int i = 0; i < (int)m_vBullets.size(); i++)
	{
		m_vBullets[i]->Release();
		delete m_vBullets[i];
	}

	m_vTowers.clear();
	m_vMonsters.clear();
	m_vBullets.clear();
	return true;
}

ObjectManager::ObjectManager(void)
{
}


ObjectManager::~ObjectManager(void)
{
	for(int i = 0; i < (int)m_vTowers.size(); i++)
	{
		m_vTowers[i]->Release();
		delete m_vTowers[i];
	}

	for(int i = 0; i < (int)m_vMonsters.size(); i++)
	{
		m_vMonsters[i]->Release();
		delete m_vMonsters[i];
	}

	for(int i = 0; i < (int)m_vBullets.size(); i++)
	{
		m_vBullets[i]->Release();
		delete m_vBullets[i];
	}
}
